package utopia_classes;

import java.util.ArrayList;
import java.util.List;

import com.example.utopia.shoppinglist.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class CustomListAdapter extends BaseAdapter {
	
	private Context context;
	public List <Shoppingitem> liste = new ArrayList <Shoppingitem>(0);
	private String Shoppinglist;
	
	public CustomListAdapter(Context context, List<Shoppingitem> ShoppingItems, String Shoppinglist){
		this.context = context;
		this.liste = ShoppingItems;
		this.Shoppinglist = Shoppinglist;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return liste.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return liste.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final Shoppingitem entry = liste.get(position);
		
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.custom_list, null);
        }
		
        TextView large = (TextView) convertView.findViewById(R.id.textView1);
        large.setText(entry.getName());

        
        TextView small = (TextView) convertView.findViewById(R.id.textView2);
        small.setText("Due to: " + entry.getDue());

        
        CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
        boolean checked = entry.isStatus();
        checkbox.setChecked(checked);
        checkbox.setFocusableInTouchMode(false);
        checkbox.setFocusable(false);
        checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        final String name = entry.getName();
        	
        	public void onCheckedChanged(CompoundButton buttonView,	boolean isChecked) {
        	// TODO Auto-generated method stub
        	if (buttonView.isChecked()) {
        	ListHandler handler = new ListHandler(context);
        	handler.callList();
        	int listnum = handler.findListe(Shoppinglist);
        	int itemnum = handler.findItem(listnum, name);
        	handler.getListe().get(listnum).getItems().get(itemnum).setStatus(true);
        	handler.saveList();
        	
        	}
        	else
        	{
            	ListHandler handler = new ListHandler(context);
            	handler.callList();
            	int listnum = handler.findListe(Shoppinglist);
            	int itemnum = handler.findItem(listnum, name);
            	handler.getListe().get(listnum).getItems().get(itemnum).setStatus(false);
            	handler.saveList();
        	}

        	}
        	}); 
       
        
        return convertView;

	}







}
